package com.monkeyboy.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.io.File;
import java.io.FileWriter;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @Author Gavin
 * @date 2021.01.16 10:32
 */
@RestController
public class TestController {
    @GetMapping("/test")
    public void test() throws Exception {
        String TEMPLATE_PREFIX = "tempfiles/";
        String TEMPLATE_SUFFIX = ".xml";
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix(TEMPLATE_PREFIX);
        resolver.setSuffix(TEMPLATE_SUFFIX);
        resolver.setCharacterEncoding("UTF-8");
        resolver.setTemplateMode("XML");
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(resolver);

        //填充数据
        Context context = new Context();
        context.setVariable("name" , "您好");
        //渲染模板生成静态
        FileWriter writer = new FileWriter("d:/temp.xml");
        templateEngine.process("temp" , context, writer);
        writer.close();
    }
}
